<template>
  <div class="bind-container">
    <div class="bind-header">
      <image
        src="/static/images/text-brand.png"
        mode="aspectFit"
      />
      <h1>绑定手机号</h1>
      <p>绑定后可使用该手机号直接登录</p>
    </div>
    <div class="bind-form">
      <van-field
        :value="mobile"
        type="number"
        title-width="auto"
        clearable
        label="+86"
        size="large"
        :maxlength="11"
        placeholder="请输入手机号"
        :error-message="errorMessage"
        @change="onChangeInput"
        :border="false"
      />
      <van-button
        type="info"
        block
        size="large"
        :disabled="disabled"
        @click="onClick"
      >获取验证码</van-button>
    </div>

    <div class="bind-footer">
      <div class="bind-footer__inner">
        获取验证码代表阅读并同意
        <a href="/pages/index/main">用户注册协议</a>
        及
        <a>隐私权保护策略声明</a>
      </div>
    </div>
  </div>
</template>

<script>
// Use Vuex
import { validateMobile } from '@/utils/validate'
import { sendcodeApi } from '@/api'
export default {
  computed: {},
  data () {
    return {
      mobile: '',
      errorMessage: '',
      disabled: true
    }
  },
  created () {
    console.log('--------Page bind created')
  },
  methods: {
    onChangeInput (e) {
      let detail = e.mp.detail
      if (detail.length === 11) {
        if (validateMobile(detail)) {
          this.errorMessage = ''
          this.disabled = false
          this.mobile = detail
        } else {
          this.errorMessage = '请输入正确的手机号码'
          this.disabled = true
        }
      } else {
        this.disabled = true
      }
    },
    onClick () {
      if (this.disabled) {
        return
      }
      sendcodeApi({ mobile: this.mobile })
      console.log('on click', this.disabled)
    }
  }
}
</script>

<style lang="less">
.bind {
  &-container {
    padding: 0 28px;
  }
  &-header {
    margin: 60px 0 70px;
    image {
      width: 30%;
      height: 20px;
      display: block;
    }
    h1 {
      font-size: 30px;
      line-height: 50px;
    }
    p {
      font-size: 12px;
      font-weight: 300;
      color: #aaa;
    }
  }
  &-form {
    .van-cell {
      background: #f7f7f7;
      border-radius: 5px;
      margin-bottom: 25px;
      &__title {
        flex: none !important;
        margin-right: 15px;
      }
    }
    .van-field__error-message {
      position: absolute;
      left: 0;
      top: 100%;
    }
    .van-button {
      border-radius: 5px !important;
    }
    .sendcode {
      background: red;
    }
  }
  &-footer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    &__inner {
      padding: 10px 28px;
      font-size: 12px;
      font-weight: 300;
      color: #ccc;
      a {
        color: #999;
        display: inline;
      }
    }
  }
}
</style>
